﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LAB.Manager
{
    public static class SynoManager
    {

        public delegate void handler(t_syno syno);
        public static event handler afterInsert;
        public static event handler afterDelete;
        public static event handler afterEdit;
        static SynoManager()
        {
           
        }


        public static List<t_syno> search(int tagId, int status, int pageIndex, int pageSize, out int count)
        {
            List<t_syno> rst = null;
            count = 0;
            using (var db = new TagerEntities())
            {
                IEnumerable<t_syno> list = db.t_syno;
                if (tagId != -1)
                {
                    list = list.Where(c => c.tagId == tagId);
                }
                if (status != -1)
                {
                    list = list.Where(c => c.status == status);
                }
                count = list.Count();
                list = list.Skip(pageIndex * pageSize).Take(pageSize);
                rst = list.ToList();
            }
            return rst;
        }

        public static int getTagIdByName(string synoName)
        {
            var rst = -1;
            using (var db = new TagerEntities())
            {
                var syno = db.t_syno.FirstOrDefault(c => c.name == synoName);
                if (syno != null)
                {
                    rst = syno.tagId;
                }
            }
            return rst;
        }

        public static List<t_syno> getSynosByTagId(int tagId)
        {
            int count = 0;
            return search(tagId, -1, 0, int.MaxValue, out count);

        }


        public static List<t_syno> getFrozenSyno(int pageIndex, int pageSize, out int count)
        {
            return search(-1, 0, pageIndex, pageSize, out count);
        }



        public static int insert(int tagId, string synoName)
        {
            var rst = -1;
            using (var db = new TagerEntities())
            {
                var tag = db.t_tag.FirstOrDefault(c => c.id == tagId && c.status == 1);
                if (tag == null)
                {
                    return -2;
                }
                var syno = db.t_syno.FirstOrDefault(c => c.name == synoName);
                if (syno != null)
                {
                    return -3;
                }

                syno = new t_syno() { tagId = tagId, name = synoName, status = 0 };
                db.t_syno.Add(syno);
                db.SaveChanges();
                rst = 0;

                // trigger
                if (afterInsert != null)
                {
                    afterInsert(syno);
                }
            }
            return rst;
        }



        public static int edit(int synoId, int status, string editor)
        {
            var rst = -1;
            using (var db = new TagerEntities())
            {
                var syno = db.t_syno.FirstOrDefault(c => c.id == synoId);
                if (syno == null)
                {
                    return -2;
                }
                if (!SysUtil.isSysEditor(editor))
                {
                    return -3;
                }
                syno.status = status;
                db.SaveChanges();
                rst = 0;
                // trigger
                if (afterEdit != null)
                {
                    afterEdit(syno);
                }
            }
            return rst;
        }

        public static int delete(int synoId)
        {
            int rst = -1;
            using (var db = new TagerEntities())
            {
                t_syno syno = db.t_syno.FirstOrDefault(c => c.id == synoId);
                if (syno == null)
                {
                    return -2;
                }
                db.t_syno.Remove(syno);
                db.SaveChanges();
                rst = 0;
                // trigger
                if (afterDelete != null)
                {
                    afterDelete(syno);
                }
            }
            return rst;

        }


    }
}
